#include "MenuItemBtn.h"

MenuItemBtn::MenuItemBtn()
    :sizeLen(QSize(70, 70))  //按钮大小默认为70*70
{
    connect(this, SIGNAL(clicked()), this, SLOT(btnClicked()));
    connect(this, SIGNAL(pressed()), this, SLOT(btnPressed()));
    //给按钮设置默认大小
    this->setFixedSize(sizeLen);
    this->setIconSize(sizeLen);
}

QSize MenuItemBtn::setBtnFixedSize(const QSize &size)
{
    this->setFixedSize(size);
    return this->size();
}

bool MenuItemBtn::setBtnStyleSheet(const QString &qss)
{
    if(qss.isEmpty()) return false;
    if(qss == this->styleSheet()) return true;
    //后期加上整个修改还是局部修改
    this->setStyleSheet(qss);
    return true;
}

bool MenuItemBtn::setBtnIcon(const QString &iconPath, const btnIconKind &kind)
{
    if(iconPath.isEmpty())
    {
        DLOG("输入iconPath为空");
        return false;
    }

    //判断传入的iconPath文件路径是否正确
    QFile file(iconPath);
    if(!file.exists())
    {
        ELOG("icon文件路径错误");
        return false;
    }
    //根据不同的kind选择对应路径进行存储
    if(kind == btnIconKind::General)
    {
        if(iconPath == this->generalIconPath) return true;
        this->generalIconPath = iconPath;
        this->setIcon(QIcon(this->generalIconPath));
    }else if(kind == btnIconKind::Clicking)
    {
        if(iconPath == this->clickingIcon) return true;
        this->clickingIcon = iconPath;
    }else if(kind == btnIconKind::Clicked)
    {
        if(iconPath == this->clickedIcon) return true;
        this->clickedIcon = iconPath;
    }else
    {
        DLOG("输入kind错误");
        return false;
    }
    return true;
}

bool MenuItemBtn::setBtnIconSize(const QSize &size)
{
    if(size.isEmpty()) return false;
    this->setIconSize(size);
    return true;
}

bool MenuItemBtn::setBtnSize(const QSize &size)
{
    if(size.width() < 1 || size.height() < 1 || size.width() > 200 || size.height() > 200)
    {
        DLOG("输入size大小不在合法范围内");
        return false;
    }
    this->sizeLen = size;
    return true;
}

bool MenuItemBtn::setBtnStatus(const btnStatus &status)
{
    if(status == btnStatus::Open)
    {
        isClick = true;
        if(clickedIcon.isEmpty()) {
            DLOG("未设置按钮打开对应Icon");
            return false;
        }
        this->setIcon(QIcon(clickedIcon));
    }else if(status == btnStatus::Close)
    {
        isClick = false;
        if(generalIconPath.isEmpty()) {
            DLOG("未设置按钮关闭对应Icon");
            return false;
        }
        this->setIcon(QIcon(generalIconPath));
    }else{
        ELOG("输入按钮状态错误!");
    }
    return true;
}

void MenuItemBtn::btnClicked()
{
    //已点击过的就不再允许被点击了
    if(isClick) return;
    isClick = !isClick;
    if(clickedIcon.isEmpty()) return;
    if(isClick) this->setIcon(QIcon(clickedIcon));
    else this->setIcon(QIcon(generalIconPath));
    emit myCliecked(this);
}

void MenuItemBtn::btnPressed()
{
    if(clickingIcon.isEmpty()) return;
    this->setIcon(QIcon(clickingIcon));
    emit myPressed();
}
